package com.lishe.lishe.lishe.module.mall.presenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.lishe.base.mvp.presenter.BaseMvpPresenter;
import com.lishe.base.common.config.Constants;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.auth.activity.AuthActivity;
import com.lishe.lishe.lishe.module.commonclass.WebActivity;
import com.lishe.lishe.lishe.module.coupon.activity.CouponActivity;
import com.lishe.lishe.lishe.module.mall.activity.ClassifySearchActivity;
import com.lishe.lishe.lishe.module.mall.activity.LoadMoreActivity;
import com.lishe.lishe.lishe.module.mall.activity.GoodsDetailActivity;
import com.lishe.lishe.lishe.module.mall.bean.GoodsItemsBean;
import com.lishe.lishe.lishe.module.mall.bean.GoodsMallResponse;
import com.lishe.lishe.lishe.module.mall.bean.HomeResponse;
import com.lishe.lishe.lishe.module.me.activity.MyAccountActivity;
import com.lishe.lishe.lishe.module.myorder.activity.MyOrdersActivity;
import com.lishe.lishe.lishe.module.recharge.activity.RechargeIntegralActivity;
import com.lishe.lishe.lishe.module.search.activity.SearchResultActivity;
import com.lishe.lishe.lishe.utils.ToastUtil;
import com.lishe.lishe.net.BaseProtocal;
import com.lishe.lishe.net.ResultListener;

import static com.lishe.lishe.lishe.module.search.activity.HomeSearchActivity.SEARCH_FROM;
import static com.lishe.lishe.lishe.module.search.activity.HomeSearchActivity.SEARCH_KEYWORD;

/**
 * 标    题：
 * 功能描述：
 * 作    者：zouxx
 * 创建时间：2018/8/13
 */

public class IndexPresenter extends BaseMvpPresenter<IndexView> {

    private IndexFragmentModule module;

    public IndexPresenter() {
        module = new IndexFragmentModule();
    }

    public void getIndexHeadData() {
        if (getMvpView() != null) getMvpView().onLoading();
        module.getIndexTopData(new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().onSuccess((HomeResponse) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().requestFailure(errcode, msg);
            }

            @Override
            public void onError(Throwable e) {

            }
        });
    }

    public void getIndexLoveData(int page) {
        if (getMvpView() != null)
            getMvpView().onLoading();
        module.getIndexLoveData(page, new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().onLoveSuccess((GoodsMallResponse) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().requestFailure(errcode, msg);
            }

            @Override
            public void onError(Throwable e) {

            }
        });
    }


    public static void setOnClick(Activity activity, GoodsItemsBean data) {
        switch (data.type) {
            case 1:// h5链接
                if (!TextUtils.isEmpty(data.linkUrl)) {
                    WebActivity.jump(activity, data.linkUrl, data.name);
                }
                break;
            case 2:// 商品
                if (!TextUtils.isEmpty(data.itemId))
                    GoodsDetailActivity.jump(activity, data.itemId);
                break;
            case 3:// 分类
                if (!TextUtils.isEmpty(data.value)) {
                    Intent intent = new Intent(activity, ClassifySearchActivity.class);
                    intent.putExtra(ClassifySearchActivity.CATEID, data.value);
                    intent.putExtra(ClassifySearchActivity.TITLE, data.name);
                    activity.startActivity(intent);
                }
                break;
            case 4:// 搜索
                if (!TextUtils.isEmpty(data.value)) {
                    Intent i = new Intent(activity, SearchResultActivity.class);
                    i.putExtra(SEARCH_KEYWORD, data.value);
                    i.putExtra(SEARCH_FROM, Constants.SEARCH_FROM_HOME);
                    activity.startActivity(i);
                }
                break;
            case 5:// 充值中心
                if (!TextUtils.isEmpty(Globals.getToken())) {
                    RechargeIntegralActivity.jump(activity);
                } else {
                    AuthActivity.jump(activity);
                }
                break;
            case 6:// 我的积分
                if (!TextUtils.isEmpty(Globals.getToken())) {
                    MyAccountActivity.jump(activity);
                } else {
                    AuthActivity.jump(activity);
                }
                break;
            case 7: // 优惠券
                if (!TextUtils.isEmpty(Globals.getToken())) {
                    activity.startActivity(new Intent(activity, CouponActivity.class));
                } else {
                    AuthActivity.jump(activity);
                }
                break;
            case 8:// 我的订单
                if (!TextUtils.isEmpty(Globals.getToken())) {
                    MyOrdersActivity.jump(activity, Globals.ORDER_TYPE_ALL);
                } else {
                    AuthActivity.jump(activity);
                }
                break;
            case 9:// 活动
            case 10:// 查看更多
                if (!TextUtils.isEmpty(data.value)) {
                    //查看更多
                    LoadMoreActivity.jump(activity, data.value, data.name);
                } else if (!TextUtils.isEmpty(data.itemId)) {
                    LoadMoreActivity.jump(activity, data.itemId, data.name);
                }
                break;
        }
    }
}
